/* Copyright Andy Mitchell 2007. No warranties */

function AsyncPopupUI(gtdinboxPage, elParent){
	this._eventObservers = new EventObservers(this);
	this._gtdinboxPage = gtdinboxPage;
	this._doc = gtdinboxPage.doc;
	
	this.create(elParent);
	
	return this;
};


AsyncPopupUI.prototype.create = function(elParent){
	this.el = this._doc.createElement("DIV");
		this.el.className = "async-popup";
	if( elParent ) elParent.appendChild( this.el );
	
	var elHeader = this._doc.createElement("DIV");
	
	
	var elHeader = this._doc.createElement("DIV");
		elHeader.className = "header";
	this.el.appendChild( elHeader );
	this.elTitle = this._doc.createElement("SPAN");
		this.elTitle.className = "title";
	elHeader.appendChild( this.elTitle );
	var elClose = this._doc.createElement("IMG");
		elClose.src = "chrome://gtdinbox/content/cross.png";
		elClose.className = "close-button";
		elClose.style.position = "absolute";
		elClose.style.right = "0px";
		elClose.style.top = "0px";
		this._eventObservers.observe(elClose, "mousedown", this.clickClose, false);
	elHeader.appendChild(elClose);
	
	this.elLoading = this._doc.createElement("DIV");
		this.elLoading.className = "loading";
		this.elLoading.style.display = "none";
	this.el.appendChild( this.elLoading );
	this.elLoading.innerHTML = "<img src='chrome://gtdinbox/skin/indicator_medium.gif'/>";
	
	this.elFrame = this._doc.createElement("IFRAME");
	this.el.appendChild( this.elFrame );
	
	this._popuper = new Popuper(this._gtdinboxPage, this.el, this);
	this._popuper.addEventHandler( this, "popupOff", this.onPopupOff );
};

AsyncPopupUI.prototype.load = function(url){
	this._id = (new Date()).valueOf();
	
	this._loading(true);
	this.elFrame.src = url + (url.indexOf("?")==-1? "?":"&") + "gtd=" + this._id;
	
	if( this._interval ){
		clearInterval( this._interval );
		this._interval = null;
	};
	this._interval = setInterval( GIPROTOTYPE.bind(this, this._loadInterval), 250 );
};
AsyncPopupUI.prototype._loadInterval = function(){
	// Test to see if loaded
	var af = this.gtdinboxPage.unsafeWindow.top._asyncFrames;
	if( af && af[this._id] ){
		clearInterval( this._interval );
		this._interval = null;
		this._loading(false);
	}
};
AsyncPopupUI.prototype._loading = function(v){
	if( typeof v=="undefined" ) return !(this.elLoading.style.display=="none");
	if( v ){
		this.elLoading.style.display = "";
		this.elFrame.style.display = "none";
	}else{
		this.elLoading.style.display = "none";
		this.elFrame.style.display = "";
	}
};


AsyncPopupUI.prototype.clickClose = function(event){
	this.popupOff();
};
AsyncPopupUI.prototype.onPopupOff = function(){
	this.destroy();
};
AsyncPopupUI.prototype.destroy = function(){
try{
	if( !this.el ) return;
	
	if( this._interval ){
		clearInterval( this._interval );
		this._interval = null;
	};
	this._popuper.destroy();
	this.el.parentNode.removeChild(this.el);
	this.el = null;
}catch(e){
	if( GTDDEBUGMODE ) alert("asyncpopup.destroy\n" + e.toString());
}
};